package indi.zhang.framework.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import indi.zhang.common.exception.ServiceException;
import indi.zhang.framework.base.mybatis.BaseServiceImpl;
import indi.zhang.framework.entity.permissionModule.Permission;
import indi.zhang.framework.mapper.PermissionMapper;
import indi.zhang.framework.service.PermissionService;
import indi.zhang.framework.tools.CurrentUserUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 总后台 权限表(Permission)表服务实现类
 *
 * @author jy
 * @since 2022-01-17 15:47:07
 */
@Service("permissionService")
public class PermissionServiceImpl extends BaseServiceImpl<PermissionMapper, Permission> implements PermissionService {

	/**
	 * 获取整个权限树
	 *
	 * @return
	 * @author jy
	 * @since 2022/1/17 15:49
	 */
	@Override
	public List<Permission> getPermissionTree() {
		LambdaQueryWrapper<Permission> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(Permission::getEnable, true).orderByAsc(Permission::getSort);
		List<Permission> permissions = baseMapper.selectList(queryWrapper);
		Map<Long, List<Permission>> collect = permissions.stream().collect(Collectors.groupingBy(Permission::getParentId));

		List<Permission> parent = collect.get(0L);
		parseList(parent, collect);
		return parent;
	}

	/**
	 * 递归拼接菜单
	 */
	private void parseList(List<Permission> parent, Map<Long, List<Permission>> permissions) {
		for (Permission permission : parent) {
			Long id = permission.getId();
			List<Permission> children = permissions.get(id);
			if (CollectionUtil.isNotEmpty(children)) {
				permission.setPChild(children);
				parseList(children, permissions);
			}
		}
	}

	/**
	 * 新增数据
	 *
	 * @param permission 实体对象
	 * @return 新增结果
	 * @author jy
	 * @since 2022-01-17 15:47:07
	 */
	@Override
	public Boolean insert(Permission permission) {
		LambdaQueryWrapper<Permission> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(Permission::getCode, permission.getCode());
		Permission exist = baseMapper.selectOne(queryWrapper);
		if (exist != null) {
			throw new ServiceException("当前权限code已存在!");
		}
		permission.setEnable(true);
		permission.setInitial(false);
		return SqlHelper.retBool(baseMapper.insert(permission));
	}

	/**
	 * 修改数据
	 *
	 * @param permission 实体对象
	 * @return 修改结果
	 * @author jy
	 * @since 2022-01-17 15:47:07
	 */
	@Override
	public boolean update(Permission permission) {
		if (StrUtil.isNotBlank(permission.getCode())) {
			LambdaQueryWrapper<Permission> queryWrapper = Wrappers.lambdaQuery();
			queryWrapper.eq(Permission::getCode, permission.getCode()).ne(Permission::getId, permission.getId());
			Permission exist = baseMapper.selectOne(queryWrapper);
			if (exist != null) {
				throw new ServiceException("当前权限code已存在!");
			}
		}
		return SqlHelper.retBool(baseMapper.updateById(permission));
	}

	/**
	 * 获取当前登录用户的权限url
	 *
	 * @return java.util.List<java.lang.String>
	 */
	@Override
	public List<String> getAllPermissionUrlByUserId() {
		return baseMapper.getAllPermissionUrlByUserId(CurrentUserUtil.getUserId());
	}

	/**
	 * 获取当前登录人的按钮权限信息
	 *
	 * @return java.util.List<java.lang.String>
	 * @author jy
	 * @since 2020/8/24 21:51
	 */
	@Override
	public List<String> getAllBtnPermissionByUserId() {
		return baseMapper.getAllBtnPermissionByUserId(CurrentUserUtil.getUserId());
	}

}

